// 钱包辅助函数
const WalletHelper = {
    // 获取MetaMask provider
    getMetaMaskProvider: function() {
        if (!window.ethereum) return null;
        
        // 多个钱包的情况
        if (window.ethereum.providers?.length) {
            const provider = window.ethereum.providers.find(p => p.isMetaMask);
            if (provider) return provider;
        }
        
        // 只有一个钱包的情况
        if (window.ethereum.isMetaMask) {
            return window.ethereum;
        }
        
        return null;
    },
    
    // 检测已安装的钱包
    detectWallets: function() {
        const wallets = [];
        
        if (!window.ethereum) return wallets;
        
        if (window.ethereum.providers?.length) {
            window.ethereum.providers.forEach(provider => {
                if (provider.isMetaMask) wallets.push('MetaMask');
                if (provider.isOkxWallet) wallets.push('OKX Wallet');
                if (provider.isCoinbaseWallet) wallets.push('Coinbase Wallet');
                if (provider.isBraveWallet) wallets.push('Brave Wallet');
            });
        } else {
            if (window.ethereum.isMetaMask) wallets.push('MetaMask');
            if (window.ethereum.isOkxWallet) wallets.push('OKX Wallet');
            if (window.ethereum.isCoinbaseWallet) wallets.push('Coinbase Wallet');
            if (window.ethereum.isBraveWallet) wallets.push('Brave Wallet');
        }
        
        return wallets;
    },
    
    // 强制使用MetaMask
    forceMetaMaskConnection: async function() {
        const provider = this.getMetaMaskProvider();
        
        if (!provider) {
            const wallets = this.detectWallets();
            if (wallets.length > 0) {
                throw new Error(`检测到 ${wallets.join(', ')}，但未找到MetaMask。请安装MetaMask或在浏览器扩展中禁用其他钱包。`);
            } else {
                throw new Error('未检测到MetaMask，请先安装MetaMask扩展。');
            }
        }
        
        return provider;
    }
};